package com.yanhui.modal.ai;

import com.yanhui.utils.ApiAccount;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TencentAISignSort {
	/**
	 * * SIGN签名生成算法-JAVA版本 通用。默认参数都为UTF-8适用 * @param HashMap
	 * <String,String> params 请求参数集，所有参数必须已转换为字符串类型 * @return 签名 * @throws
	 * IOException
	 */
	public static String getSignature(Map<String, String> params) throws IOException {
		Map<String, String> sortedParams = new TreeMap<String, String>(params);
		Set<Map.Entry<String, String>> entrys = sortedParams.entrySet();
		StringBuilder baseString = new StringBuilder();
		for (Map.Entry<String, String> param : entrys) {
			System.out.println("=="+param.getKey().trim());
			if (param.getValue() != null && !"".equals(param.getKey().trim()) && !"sign".equals(param.getKey().trim())
					&& !"".equals(param.getValue().trim())) {
				baseString.append(param.getKey().trim()).append("=")
				.append(URLEncoder.encode(param.getValue().trim(), "UTF-8")).append("&");
							}
		}
		if (baseString.length() > 0) {
			baseString.deleteCharAt(baseString.length() - 1).append("&app_key=" + ApiAccount.TENCENT_APP_KEY_AI);
		}
		try {
			MD5 md5 = new MD5();
			String sign = md5.getMD5(baseString.toString());
			return sign;
		} catch (Exception ex) {
			throw new IOException(ex);
		}
	}

	/**
	 * * SIGN签名生成算法-JAVA版本 针对于基本文本分析接口要求text为GBK的方法 * @param HashMap
	 * <String,String> params 请求参数集，所有参数必须已转换为字符串类型 * @return 签名 * @throws
	 * IOException
	 */
	public static String getSignatureforNLP(Map<String, String> params) throws IOException {
		Map<String, String> sortedParams = new TreeMap<String, String>(params);
		Set<Map.Entry<String, String>> entrys = sortedParams.entrySet();
		StringBuilder baseString = new StringBuilder();
		for (Map.Entry<String, String> param : entrys) {
			if (param.getValue() != null && !"".equals(param.getKey().trim()) && !"sign".equals(param.getKey().trim())
					&& !"".equals(param.getValue().trim())) {
				if (param.getKey().equals("text")) {
					baseString.append(param.getKey().trim()).append("=")
					.append(URLEncoder.encode(param.getValue().trim(), "GBK")).append("&");
					} else {
					baseString.append(param.getKey().trim()).append("=")
							.append(URLEncoder.encode(param.getValue().trim(), "UTF-8")).append("&");
				}
			}
		}
		if (baseString.length() > 0) {
			baseString.deleteCharAt(baseString.length() - 1).append("&app_key=" + ApiAccount.TENCENT_APP_KEY_AI);
		}
		try {
			MD5 md5 = new MD5();
			String sign = md5.getMD5(baseString.toString());
			return sign;
		} catch (Exception ex) {
			throw new IOException(ex);
		}
	}

	/**
	 * * 获取拼接的参数 * @param params * @return * @throws IOException
	 */
	public static String getParams(HashMap<String, String> params) throws IOException {
		Map<String, String> sortedParams = new TreeMap<String, String>(params);
		Set<Map.Entry<String, String>> entrys = sortedParams.entrySet();
		StringBuilder baseString = new StringBuilder();
		for (Map.Entry<String, String> param : entrys) {
			baseString.append(param.getKey().trim()).append("=")
			.append(URLEncoder.encode(param.getValue().trim(), "UTF-8")).append("&");
		}
		return baseString.toString();
	}
	public static String getParams(Map<String, String> params) throws IOException {
		Map<String, String> sortedParams = new TreeMap<String, String>(params);
		Set<Map.Entry<String, String>> entrys = sortedParams.entrySet();
		StringBuilder baseString = new StringBuilder();
		for (Map.Entry<String, String> param : entrys) {
			baseString.append(param.getKey().trim()).append("=")
			.append(URLEncoder.encode(param.getValue().trim(), "UTF-8")).append("&");
		}
		return baseString.toString();
	}
}
